#!/bin/sh
#

echo "Installing packages necessary to compile openSSH..."
zypper install --no-recommends --auto-agree-with-licenses --type package make gcc zlib-devel libopenssl-devel

# Make sure that you stop any instances of the current version of the server
# or you will not be able to startup the new one because the port will be in use
echo "Stopping current sshd..."
/etc/init.d/sshd stop

OPENSSHHOME=/home/openssh
OPENSSHV=5.5p1

mkdir -p $OPENSSHHOME
cd $OPENSSHHOME
if ! [ -d "openssh-$OPENSSHV" ] ; then
  echo "Downloading version $OPENSSHV of openSSH... "
  (cd $OPENSSHHOME; wget "http://filedump.se.rit.edu/pub/OpenBSD/OpenSSH/portable/openssh-${OPENSSHV}.tar.gz.asc" )
  (cd $OPENSSHHOME; wget "http://filedump.se.rit.edu/pub/OpenBSD/OpenSSH/portable/openssh-${OPENSSHV}.tar.gz" )
#  pgp --verify ./openssh-${OPENSSHV}.tar.gz.asc ./openssh-${OPENSSHV}.tar.gz
  (cd $OPENSSHHOME; tar zxf ./openssh-${OPENSSHV}.tar.gz )
  (cd $OPENSSHHOME; ln -s ./openssh-${OPENSSHV} ./openssh )
fi
echo "Configuring openSSH..."
(cd $OPENSSHHOME/openssh-${OPENSSHV} ; ./configure )
echo "Building openSSH..."
(cd $OPENSSHHOME/openssh-${OPENSSHV} ; make )
echo "Installing openSSH..."
(cd $OPENSSHHOME/openssh-${OPENSSHV} ; make install )

# Install the init.d script that assumes /usr/local for the install directory
# This should be confirmed with the text at the end of the ./configure run...
echo "Saving and installing configuration files..."
mv /etc/init.d/sshd /etc/init.d/sshd.release
cp ./sshd /etc/init.d
# The release conf file is stored in /etc not /usr/local/etc so we don't need to move it.
# We do need to wait till we have installed the new sshd before we can copy it so
# that it overwrites the default one.
cp ./sshd_conf /usr/local/etc

echo "Starting new SSHD..."
/etc/init.d/sshd start
